package com.kg.rails.component.editor.shape;

import com.kg.rails.component.editor.canvas.Graphics2DCanvas;
import com.kg.rails.component.editor.util.Constants;
import com.kg.rails.component.editor.util.EditorRectangle;
import com.kg.rails.component.editor.util.EditorUtils;
import com.kg.rails.component.editor.view.CellState;

import java.awt.*;

public class SwimlaneShape extends BasicShape {

    /**
     *
     */
    public void paintShape(Graphics2DCanvas canvas, CellState state) {
        int start = (int) Math.round(EditorUtils.getInt(state.getStyle(),
                Constants.STYLE_STARTSIZE, Constants.DEFAULT_STARTSIZE)
                * canvas.getScale());

        Rectangle tmp = state.getRectangle();

        if (EditorUtils
                .isTrue(state.getStyle(), Constants.STYLE_HORIZONTAL, true)) {
            if (configureGraphics(canvas, state, true)) {
                canvas.fillShape(new Rectangle(tmp.x, tmp.y, tmp.width, Math
                        .min(tmp.height, start)));
            }

            if (configureGraphics(canvas, state, false)) {
                canvas.getGraphics().drawRect(tmp.x, tmp.y, tmp.width,
                        Math.min(tmp.height, start));
                canvas.getGraphics().drawRect(tmp.x, tmp.y + start, tmp.width,
                        tmp.height - start);
            }
        } else {
            if (configureGraphics(canvas, state, true)) {
                canvas.fillShape(new Rectangle(tmp.x, tmp.y, Math.min(
                        tmp.width, start), tmp.height));
            }

            if (configureGraphics(canvas, state, false)) {
                canvas.getGraphics().drawRect(tmp.x, tmp.y,
                        Math.min(tmp.width, start), tmp.height);
                canvas.getGraphics().drawRect(tmp.x + start, tmp.y,
                        tmp.width - start, tmp.height);
            }
        }

    }

    /**
     *
     */
    protected EditorRectangle getGradientBounds(Graphics2DCanvas canvas,
                                                CellState state) {
        int start = (int) Math.round(EditorUtils.getInt(state.getStyle(),
                Constants.STYLE_STARTSIZE, Constants.DEFAULT_STARTSIZE)
                * canvas.getScale());
        EditorRectangle result = new EditorRectangle(state);

        if (EditorUtils
                .isTrue(state.getStyle(), Constants.STYLE_HORIZONTAL, true)) {
            result.setHeight(Math.min(result.getHeight(), start));
        } else {
            result.setWidth(Math.min(result.getWidth(), start));
        }

        return result;
    }

}
